package com.example.secondhandshop.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.secondhandshop.R;
import com.example.secondhandshop.model.AddOrderModel;

public class PurchaseActivity extends AppCompatActivity {

    private TextView titleTextView;
    private TextView priceTextView;
    private TextView userNameTextView;
    private ImageView sellerAvatarImageView;
    private ImageView itemImageView;
    private Button addOrderButton;
    private TextView bottomPriceTextView;
    private TextView locationText;


    private long buyerId=624;
    private long sellerId;
    private long itemId;
    private int price;
    private String title;
    private String sellerAvatarUrl;
    private String itemImageUrl;
    private String userName;
    private String addr;

    private AddOrderModel addOrderModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);

        // 初始化视图
        titleTextView = findViewById(R.id.titleText);
        priceTextView = findViewById(R.id.priceText);
        userNameTextView = findViewById(R.id.userNameText);
        sellerAvatarImageView = findViewById(R.id.sellerAvatar);
        itemImageView = findViewById(R.id.itemImage);
        bottomPriceTextView = findViewById(R.id.priceBottomText);
        addOrderButton = findViewById(R.id.AddOrderButton);
        locationText = findViewById(R.id.locationText);


        // 获取传递的数据
        Intent intent = getIntent();
        itemId = intent.getLongExtra("itemId", -1); // 获取商品ID
//        buyerId = intent.getLongExtra("buyerId", -1); // 获取买家ID

        price = intent.getIntExtra("price", 0); // 获取商品价格
        sellerId = intent.getLongExtra("sellerId", -1); // 获取卖家ID
        title = intent.getStringExtra("title"); // 获取商品标题
        sellerAvatarUrl = intent.getStringExtra("magic"); // 获取卖家头像URL
        itemImageUrl = intent.getStringExtra("image");// 获取商品图片URL
        userName = intent.getStringExtra("username");// 获取卖家用户名
        addr = intent.getStringExtra("addr");// 获取卖家地址



        // 检查获取的数据是否有效
        if (itemId == -1 || buyerId == -1 || sellerId == -1 || title == null ) {
            Log.e("PurchaseActivity", "接收到的数据不完整");
            Toast.makeText(this, "订单信息获取失败", Toast.LENGTH_SHORT).show();
            finish(); // 关闭活动
            return;
        }

        // 设置数据到 UI
        titleTextView.setText(title);
        priceTextView.setText("￥"+price);
        bottomPriceTextView.setText("￥"+price);
        userNameTextView.setText(userName);
        if(addr!=null) {locationText.setText(addr);}

        Glide.with(this)
                .load(sellerAvatarUrl)
                .placeholder(R.drawable.touxiang) // placeholder 头像
                .into(sellerAvatarImageView);

        Glide.with(this)
                .load(itemImageUrl)
                .placeholder(R.drawable.unknown_goods)
                .into(itemImageView);

        // 提交按钮监听
       addOrderButton.setOnClickListener(view -> {

           Log.d("PurchaseActivity", "点击了提交订单按钮");
            confirmPurchase();
        });
    }

    private void confirmPurchase() {
        // 调用 API 提交订单
        addOrderModel = new AddOrderModel();
        addOrderModel.postOrder(buyerId, itemId, price, sellerId, new AddOrderModel.OnProductsFetchedListener() {

            @Override
            public void onSuccess() {
                Log.d("PurchaseActivity", "提交订单api调用成功");
                runOnUiThread(() -> showSuccessDialog());
                finish();

            }

            @Override
            public void onFailure(String errorMessage) {
                runOnUiThread(() ->  Toast.makeText(PurchaseActivity.this, "购买失败: " + errorMessage, Toast.LENGTH_SHORT).show());
            }
        });
    }

    private void showSuccessDialog() {
        new AlertDialog.Builder(PurchaseActivity.this)
                .setTitle("购买成功")
                .setMessage("您的订单已成功提交！")
                .setPositiveButton("确定", (dialog, which) -> {
                    dialog.dismiss();
                    finish();
                })
                .show();
    }
}